home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7821 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.8 KB  |  83 lines

  1. Path: sun001.spd.dsccc.com!spd!jmccarty
  2. From: jmccarty@spd.dsccc.com (Mike McCarty)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: line intersection in C
  5. Date: 29 Feb 1996 02:10:46 GMT
  6. Organization: DSC Communications Corporation, Plano, Texas USA
  7. Message-ID: <4h31v6$68q@sun001.spd.dsccc.com>
  8. References: <1996Feb25.230142.29689@dcs.warwick.ac.uk> <3134933A.AB9@computek.net>
  9. NNTP-Posting-Host: aplo139.spd.dsccc.com
  10.  
  11. In article <3134933A.AB9@computek.net>,
  12. robert jacobs and jason jacobs  <robertj@computek.net> wrote:
  13. )Daniel Castillo Molero wrote:
  14. )> 
  15. )> Hello,
  16. )> 
  17. )> I wonder if anybody can help me to find an algorithm in C to detect whether
  18. )> two line segments intersect properly (I don't know if this is the correct
  19. )> terminology, but by proper intersection I mean that the intersection is
  20. )> not in the extreme of any of them and that one does not lie on top of
  21. )> the other).
  22. )> I need to test intersection just for line segments whose slope is a multiple
  23. )> of 45 degrees, which I suppose may simplify the solution.
  24. )> 
  25. )> I would greatly appreciate any sort of help.
  26. )> 
  27. )> Daniel Castillo.
  28. )> 
  29. )> danmol@dcs.warwick.ac.uk
  30. )> 
  31. )> --
  32. )> * Daniel Castillo.  D.C.Molero@dcs.warwick.ac.uk *
  33. )
  34. )Plug the end points of line segements into the equation for a line.
  35. )Calculate the slope of the lines.  If the slopes are defferent the lines 
  36. )will intersect.
  37. )
  38. )Bob Jacobs
  39.  
  40.  
  41. Bob, you really should have read the post before responding to it. They
  42. guy is talking about line segments. This is not a simple problem.
  43.  
  44. Here's a try:
  45.  
  46. First, sort the endpoints of each of the two lines by x coordinates. If
  47. the largest x for the first line is less than the smallest x for the
  48. other, then there is no intersection (takes two checks). You can also
  49. check the y's the same way.
  50.  
  51. Now, since the points are sorted by x's, you can check the slopes by
  52. just seeing the direction of change. If the slopes are the same, then
  53. no "proper" intersection is possible.
  54.  
  55. Now comes the hard part, I guess. I think that the best way may be to
  56. use distances. Compute the equation of one of the lines. Find the
  57. distances from the endpoints of the other line to the line you
  58. computed. If one is negative, and the other is positive, you have a
  59. real intersection. If they are of the same sign, then you have no
  60. intersection. If one of the distances is zero, then you have a
  61. degenerate T intersection.
  62.  
  63. If a line has equation 
  64.  
  65.     Ax + By + C = 0
  66.  
  67. then the distance function of a given point (x,y) to the line is
  68.  
  69.     distance(x,y) = Ax + By + C
  70.  
  71. If the distance is 0, then the point is on the line, don't you see.
  72.  
  73. This is something I just came up with as fast as I could type. There may
  74. be (probably is?) a better way.
  75.  
  76. Mike
  77.  
  78.  
  79. ----
  80. char *p="char *p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
  81.  
  82. I don't speak for DSC.         <- They make me say that.
  83.